package proyectobdii;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.LinkedList;
import java.util.StringTokenizer;


public class Controller_Procedure {
    
    private LinkedList<Procedure> exlusiveProcedureDBI;
    private LinkedList<Procedure> exlusiveProcedureDBII;
    private Controller_Connection ctrl_connection;
    private LinkedList<Procedure> commonProcedures = null;
    private LinkedList<Procedure> diffProcedures = null;
    private Diff_Procedure diff_procedure;
    
    public Controller_Procedure(Controller_Connection ctrl_connection) throws Exception{
    	exlusiveProcedureDBI = new LinkedList<Procedure>();
    	exlusiveProcedureDBII = new LinkedList<Procedure>();
        this.ctrl_connection = ctrl_connection;
        
        DataBase connectionI = DataBase.getInstance(ctrl_connection.getDataBaseI().getUserName(),ctrl_connection.getDataBaseI().getPassword(),ctrl_connection.getDataBaseI().getHost(), ctrl_connection.getDataBaseI().getDataBaseName());
        DatabaseMetaData metaI = connectionI.getConection().getMetaData();
        
        ResultSet resultNameProcI = metaI.getProcedures(null, connectionI.getDataBaseName(), null);
        ResultSet resultI = metaI.getProcedureColumns(null, connectionI.getDataBaseName() ,"%", null);

        while(resultNameProcI.next()) {
            LinkedList <Parameter> auxParametro = new LinkedList<Parameter>();
            while (resultI.next()){
                auxParametro.add(new Parameter(resultI.getString(4),resultI.getString(7),resultI.getString(6)));
            }
            exlusiveProcedureDBI.add(new Procedure(resultNameProcI.getString(3), auxParametro, "Ninguno"));
        }
       
        //DataBase connectionII = DataBase.getInstance(ctrl_connection.getDataBaseII().getUserName(),ctrl_connection.getDataBaseII().getPassword(),ctrl_connection.getDataBaseII().getHost(), ctrl_connection.getDataBaseII().getDataBaseName());
        DataBase connectionII = new DataBase(ctrl_connection.getDataBaseII().getUserName(),ctrl_connection.getDataBaseII().getPassword(),ctrl_connection.getDataBaseII().getHost(), ctrl_connection.getDataBaseII().getDataBaseName());
        connectionII.connect();
        
        DatabaseMetaData metaII = connectionII.getConection().getMetaData();
        ResultSet resultNameProcII = metaII.getProcedures(null, connectionII.getDataBaseName(), null);
        ResultSet resultII = metaII.getProcedureColumns(null, connectionII.getDataBaseName() ,"%", null); 
        
        while(resultNameProcII.next()) {
            LinkedList <Parameter> auxParametro = new LinkedList<Parameter>();
            while (resultII.next()){
               auxParametro.add(new Parameter(resultII.getString(4),resultII.getString(7),resultII.getString(6)));
            }
            exlusiveProcedureDBII.add(new Procedure(resultNameProcII.getString(3), auxParametro, "Ninguno"));
                       
        }
        
        separarProcedimientos();
        diff_procedure = new Diff_Procedure(ctrl_connection.getDataBaseI(), connectionII, 
                    commonProcedures, exlusiveProcedureDBI, exlusiveProcedureDBII, diffProcedures);
        
    }
    
    public Diff_Procedure getDiffProcedure(){
    	return diff_procedure;
    }
    
   private void separarProcedimientos(){
        LinkedList<Procedure> iguales = new LinkedList<Procedure>();
        LinkedList<Procedure> diferentes = new LinkedList<Procedure>();
        
        for(int i=0;i<exlusiveProcedureDBI.size();i++){
            for(int j=0;j <exlusiveProcedureDBII.size();j++){
                if(exlusiveProcedureDBI.get(i).compareTo(exlusiveProcedureDBII.get(j))==0){
                    iguales.add(exlusiveProcedureDBI.get(i));
                    i++;
                    

                }else if(exlusiveProcedureDBI.get(i).compareTo(exlusiveProcedureDBII.get(j))!=0){
                    diferentes.add(exlusiveProcedureDBI.get(i));
                    diferentes.add(exlusiveProcedureDBII.get(j));
                    
                }else{
                    j++;
                }
            }
        }
        commonProcedures=iguales;
        diffProcedures=diferentes;
    }
}
